# -*- coding:Utf-8 -*-
'''
Created on Dec 7, 2012

@author: fefoley
'''

import pyicl
from datetime import *

import myio
from timestrip import DatetimeInterval, DatetimeIntervalSet, Timestrip, TimestripSet

if __name__=="__main__":
    
    # file constantes - could be automated using an explorer to choose file
    input_root_path = "/home/test-dev/reverse_hours.io/"
    path_to_configfile = input_root_path + "reverse_hours.config.txt"
        
    "importation des paramètres de config"
    config = myio.import_config(path_to_configfile)

    'importation du scope de config'
    wdstart = config.get('workday','startday')
    wdend = config.get('workday','endday')

    'importation des setup et setdown times pour écrire un mail'
    setup = int(config.get('setuptime','mailstart'))
    setdown = int(config.get('setuptime','mailend'))
    
    "importation des données dans des listes"
    lstmails = myio.import_mails(config) # importer la liste des mails et retourne une liste d'objet Mail
#    lstmeetings = myio.import_meetings(config) # importer la liste des réunions et retourne une liste d'objet Meeting
#    lstsentmeetings = myio.import_sentmeetings(config)
#    lstholidays = myio.import_holidays(config) # importer la liste des jours fériés et retourne une liste d'objet datetime.date
#    lstdaysoff = myio.import_daysoff(config) # importer la liste des congés et retourne une liste d'objet datetime.date

    
#    lstmeetings = lstmeetings[100:200]
#    lstsentmeetings = lstsentmeetings[100:200]
#    lstmails = lstmails[100:300]

#    dtset = DatetimeIntervalSet([DatetimeInterval(meeting.Start, meeting.End) for meeting in lstmeetings])
#    dtset.add([DatetimeInterval(sentmeeting.SentOn + timedelta(minutes=-10), 
#                                sentmeeting.SentOn + timedelta(minutes=+10)) for sentmeeting in lstsentmeetings])

    'scope of work'
    scope = DatetimeIntervalSet()
    scope.add(DatetimeInterval(datetime.strptime(wdstart, "%d/%m/%Y"), 
                               datetime.strptime(wdend, "%d/%m/%Y") + timedelta(days=1)))
    
    'TimestripSet of mails - non aggregated if .insert(), but faster'
    ts_set = TimestripSet()
    ts_set.insert([Timestrip(mail.SentOn - timedelta(minutes=setup), mail.SentOn + timedelta(minutes=setdown), mail ) for mail in lstmails])    

    ts_set = ts_set & scope
    ts_set.aggregate()
    print("mails sent on (scope:\n{0}".format(ts_set)) 
    
    'set for holidays and daysoff, reduced with scope'
#    holidays_set = DatetimeIntervalSet([DatetimeInterval(day, day + timedelta(days=1)) for day, _ in lstholidays]) & scope
#    daysoff_set = DatetimeIntervalSet([DatetimeInterval(day1, day2 + timedelta(days=1)) for day1, day2, _, _ in lstdaysoff]) & scope

#    alloff_set = DatetimeIntervalSet()
#    alloff_set.add(holidays_set)
#    alloff_set.add(daysoff_set) 


#    mail_set = dtsetmails & daysoff_set
#    print("mails sent on daysoff_set:\n{0}".format(mail_set))  
#    mail_set.aggregate()
#    print("aggregaated mails :\n{0}".format(mail_set))      
    
#    hours_ON_set = compute_hours_ON()
#    hours_OFF_set = compute_hours_OFF()
    
    
    
#    
    
    
    